using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class ucDichVu : BaseUserControl
    {
        private DAL.AccessData m = new DAL.AccessData();
        private string sql = "";
        private int iHeght = 32, iWidth = 250;
        public DataTable dtct = new DataTable();
        public DataTable dtthem = new DataTable();
        private DataTable dtvp = new DataTable();
        public ucDichVu(DataTable dt)
        {
            InitializeComponent();
            this.dtct = dt;
            dtthem = dtct.Clone();
            dtthem.Clear();
        }

        private void frmDichvu_Load(object sender, EventArgs e)
        {
            TreeNode node;
            sql = "select id,ma,ten,stt from "+m.user+".v_loaivp order by stt";
            foreach (DataRow r in m.get_data(sql).Tables[0].Rows)
            {
                node = new TreeNode(r["ten"].ToString());
                node.Tag = r["id"].ToString();
                treeView1.Nodes.Add(node);
            }
        }
        private void load_giavp(int id_loai)
        {
            sql = "select id,ma,ten,dvt,stt,gia_th from "+m.user+".v_giavp where id_loai="+id_loai+" order by stt";
            CheckBox chk;
            int i = 0, l = 3, t = 6;
            dtvp=m.get_data(sql).Tables[0];
            if (dtvp.Rows.Count <= 17) iWidth = 600;
            else if (dtvp.Rows.Count <= 34) iWidth = 300;
            else iWidth = 250;
            foreach (DataRow r in dtvp.Rows)
            {
                if (i != 0 && i % 17 == 0)
                {
                    t = 6;
                    l = iWidth + 20;
                }
                chk = new CheckBox();
                chk.Name = "chk" + (i+1).ToString();
                chk.Text = r["ten"].ToString()+" ("+r["gia_th"].ToString()+")";
                chk.Tag = r["id"].ToString();
                chk.CheckedChanged += new System.EventHandler(this.chk_CheckedChanged);
                chk.Size = new System.Drawing.Size(iWidth, iHeght);
                chk.Top = t;
                chk.Left = l;
                if (m.getrowbyid(dtct, "id=" + int.Parse(r["id"].ToString())) == null)
                {
                    pGiavp.Controls.Add(chk);
                    i++;
                    t += 30;
                }
            }
            pGiavp.AutoScroll = true;
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByKeyboard || e.Action == TreeViewAction.ByMouse)
            {
                int id_loai = 0;
                try
                {
                    id_loai = int.Parse(e.Node.Tag.ToString());
                    e.Node.ForeColor = Color.Red;
                }
                catch { id_loai = 0; }
                if (id_loai == 0) return;
                pGiavp.Controls.Clear();
                load_giavp(id_loai);
            }
        }

        private void chk_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            int i_mavp = int.Parse(chk.Tag.ToString());
            DataRow row=m.getrowbyid(dtvp,"id="+i_mavp);
            if (row != null)
            {
                DataRow r = m.getrowbyid(dtct, "id=" + i_mavp);
                if (r == null)
                {
                    m.updrec_chidinh(dtct, i_mavp, get_stt(dtct), row["ma"].ToString(), row["ten"].ToString(), row["dvt"].ToString(), 1, decimal.Parse(row["gia_th"].ToString()));
                    m.updrec_chidinh(dtthem, i_mavp, get_stt(dtthem), row["ma"].ToString(), row["ten"].ToString(), row["dvt"].ToString(), 1, decimal.Parse(row["gia_th"].ToString()));
                }
                else if (!chk.Checked)
                {
                    m.delrec(dtct, "id=" + i_mavp);
                    m.delrec(dtthem, "id=" + i_mavp);
                }
            }
            dtct.AcceptChanges();
            dtthem.AcceptChanges();
        }
        private int get_stt(DataTable dt)
        {
            int _stt = 0, var = 0;
            try
            {
                foreach (DataRow r in dt.Select("true", "stt"))
                {
                    _stt = int.Parse(r["stt"].ToString());
                    if ((var + 1) != _stt)
                    {
                        var = var + 1;
                        return var;
                    }
                    else var = _stt;
                }
                var += 1;
            }
            catch { var += 1; }
            return var;
        }

        private void butLuu_Click(object sender, EventArgs e)
        {

        }

        private void frmDichvu_KeyDown(object sender, KeyEventArgs e)
        {
            //fix:
            //if (e.KeyCode == Keys.Escape)
                //this.Close();
        }
    }
}